<template>
  <div class="app-container">
    <!-- 搜素 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="标题：">
        <el-input v-model="searchForm.title" size="small" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="操作人">
        <el-input v-model="searchForm.operName" size="small" placeholder="请输入操作人" />
      </el-form-item>
      <el-form-item label="状态：">
        <el-select v-model="searchForm.status" size="small" placeholder="请选择操作状态">
          <el-option label="操作成功" value="0" />
          <el-option label="操作失败" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" icon="el-icon-search" @click="onSubmit">查询</el-button>
        <el-button size="small" icon="el-icon-refresh-left" @click="restBtn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮组 -->
    <el-row>
      <el-button type="primary" size="mini" icon="el-icon-bottom" plain @click="exportExcel">导出</el-button>
      <el-button type="danger" size="mini" icon="el-icon-delete" plain @click="deleteAll">清空</el-button>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="logList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日志编号" prop="id" width="100" />
      <el-table-column label="标题" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="操作类型" prop="requestMethod" :show-overflow-tooltip="true" />
      <el-table-column label="操作地点" prop="operLocal" :show-overflow-tooltip="true" />
      <el-table-column label="操作IP" prop="operIp" :show-overflow-tooltip="true" />
      <el-table-column label="操作人" prop="operName" :show-overflow-tooltip="true" />
      <el-table-column label="请求地址" prop="openUrl" :show-overflow-tooltip="true" />
      <el-table-column label="操作状态" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status === 0?'正常':'失败' }}
        </template>
      </el-table-column>
      <el-table-column label="登录时间" prop="operTime" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="showLogInfo(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="searchForm.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="searchForm.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 详情 -->
    <el-dialog
      title="详情信息"
      :visible.sync="logInfoShow"
      width="60%"
    >
      <div class="logInfo-item">
        <strong>操作标题：</strong>
        <span>{{ logInfo.title }}</span>
      </div>
      <div class="logInfo-item">
        <strong>方法名称：</strong>
        <span>{{ logInfo.methodName }}</span>
      </div>
      <div class="logInfo-item">
        <strong>请求方式：</strong>
        <span>{{ logInfo.requestMethod }}</span>
      </div>
      <div class="logInfo-item">
        <strong>操作人：</strong>
        <span>{{ logInfo.operName }}</span>
      </div>
      <div class="logInfo-item">
        <strong>请求地址：</strong>
        <span>{{ logInfo.openUrl }}</span>
      </div>
      <div class="logInfo-item">
        <strong>详细地址：</strong>
        <span>{{ logInfo.operLocal }}</span>
      </div>
      <div class="logInfo-item">
        <strong>IP地址：</strong>
        <span>{{ logInfo.operIp }}</span>
      </div>
      <div class="logInfo-item">
        <strong>返回参数：</strong>
        <span>{{ logInfo.jsonResult }}</span>
      </div>
      <div class="logInfo-item">
        <strong>异常信息：</strong>
        <span>{{ logInfo.errMsg }}</span>
      </div>
      <div class="logInfo-item">
        <strong>操作状态：</strong>
        <span>{{ logInfo.status }}</span>
      </div>
      <div class="logInfo-item">
        <strong>操作时间：</strong>
        <span>{{ logInfo.operTime }}</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="logInfoShow = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getOperList, deleteAllOper } from '@/api/log'
import { exportExcel } from '@/utils/excel'
export default {
  name: 'UiLogin',
  data() {
    return {
      logInfoShow: false,
      searchForm: {
        title: '',
        operName: '',
        status: '',
        page: 1,
        limit: 10
      },
      total: 0,
      logList: [],
      loading: true,
      baseUrl: process.env.VUE_APP_BASE_API,
      logInfo: {
        title: '',
        methodName: '',
        requestMethod: '',
        operName: '',
        openUrl: '',
        operLocal: '',
        operIp: '',
        jsonResult: '',
        errMsg: '',
        status: '',
        operTime: ''
      }

    }
  },
  created() {
    this.getLoginListData()
  },
  methods: {
    // 获取日志数据
    getLoginListData() {
      getOperList(this.searchForm).then(res => {
        const { data } = res
        this.logList = data.data
        this.total = data.total
        this.loading = false
      })
    },
    // 搜素
    onSubmit() {
      this.getLoginListData()
    },
    // 改变页面大小
    handleSizeChange(val) {
      this.searchForm.limit = val
      this.getLoginListData()
    },
    // 前去第几页
    handleCurrentChange(val) {
      this.searchForm.page = val
      this.getLoginListData()
    },
    // 重置搜索按钮
    restBtn() {
      this.searchForm.title = ''
      this.searchForm.operName = ''
      this.searchForm.status = ''
      this.getLoginListData()
    },
    // 导出Excel
    exportExcel() {
      this.$confirm('确定导出所有日志？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        exportExcel('/logger/oper/export')
      })
    },
    deleteAll() {
      this.$confirm('确定清空所有记录？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAllOper().then(res => {
          this.$message({ message: res.msg, type: 'success' })
          this.getLoginListData()
        })
      })
    },
    // 打开详情信息
    showLogInfo(data) {
      this.logInfoShow = true
      this.logInfo.title = data.title
      this.logInfo.methodName = data.methodName
      this.logInfo.requestMethod = data.requestMethod
      this.logInfo.operName = data.operName
      this.logInfo.openUrl = data.openUrl
      this.logInfo.operTime = data.operTime
      this.logInfo.status = data.status === 0 ? '操作正常' : '操作失败'
      this.logInfo.errMsg = data.errMsg
      this.logInfo.jsonResult = data.jsonResult
      this.logInfo.operIp = data.operIp
      this.logInfo.operLocal = data.operLocal
    }
  }
}
</script>

<style lang="scss" scoped>
	.logInfo-item{
		margin: 25px 20px;
	}
	.el-dialog{
		font-size: 14px;
	}
</style>
